﻿// HSS.Interlink.Web.InterlinkUtilities.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       InterlinkUtilities.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
namespace HSS.Interlink.Web
{
	#region Using Directives
	using System;
	using System.IO;
	using System.Reflection;
	using System.Security.Permissions;
	using System.Web;
	using System.Web.Hosting;
	using Microsoft.Win32;
	using System.Threading;
	#endregion

	#region InterlinkUtilities
	/// <summary>
	/// Misc. file utility methods.
	/// </summary>
	public static class InterlinkUtilities
	{

		/// <summary>
		/// Gets the file Content-Type Description based on the file name and extension from the Windows Registry.
		/// </summary>
		/// <param name="fileName">The name of the file to get the Description for. (Example: file.ext)</param>
		/// <returns>The file description if found; otherwise a plain string containing 'User File'</returns>
		/// <exception cref="System.Security.SecurityException">A caller higher in the call stack 
		/// does not have the permission specified by the current instance.-or- A caller higher in the call stack has called 
		/// System.Security.CodeAccessPermission.Deny() on the current permission object.</exception>
		public static string GetContentTypeDescription(string fileName)
		{
			string defaultDescription = "User file";
			RegistryKey typeKey = null;
			try
			{
				string ext = Path.GetExtension(fileName);
				if (string.IsNullOrEmpty(ext))
					return defaultDescription;
				ext = ext.ToLower();

				try
				{
					RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, @"\\HKEY_CLASSES_ROOT");
					regPerm.Demand();
				}
				catch
				{
					return defaultDescription;
				}

				typeKey = Registry.ClassesRoot.OpenSubKey(ext);
				if (null != typeKey)
				{
					string fileType = typeKey.GetValue(string.Empty).ToString();
					typeKey.Close();
					typeKey = Registry.ClassesRoot.OpenSubKey(fileType);
				}

				if (null != typeKey)
				{
					defaultDescription = typeKey.GetValue(string.Empty).ToString();
				}
			}
			catch { }
			finally
			{
				if (null != typeKey)
					typeKey.Close();
			}

			return defaultDescription;
		}
		/// <summary>
		/// Gets a folder relative to the ApplicationPhysicalPath.
		/// </summary>
		/// <param name="folderName">The name of the folder</param>
		/// <returns>The folder's full path</returns>
		public static string GetFolder(string folderName)
		{
			string folder = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, folderName);
			if (!Directory.Exists(folder))
				Directory.CreateDirectory(folder);
			return folder;
		}

		internal static string GetAppRelativeTempFolder(string folderName)
		{
			if (string.IsNullOrEmpty(folderName))
				throw new ArgumentNullException("folderName");
			string path = GetFolder(Path.Combine("Temp", folderName));
			if (!Directory.Exists(path))
				Directory.CreateDirectory(path);
			return path;
		}
	}
	#endregion
}